package com.isoft.student.service.impl;

import com.isoft.student.dao.PersonMapper;
import com.isoft.student.dao.TeacherMapper;
import com.isoft.student.eo.TeacherEOPage;
import com.isoft.student.model.Teacher;
import com.isoft.student.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 实现业务及流程接口当中的内容
 */
@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private PersonMapper personMapper;
    /**
     * 教师信息添加方式实现
     * @param teacher 教师信息实体
     * @return 是否成功（-1 重复添加）
     */
    @Override
    public int AddTeacher(Teacher teacher) {
        int returnInt=0;
        //业务判断
        //（1）教师信息名称不能重复

        //(1)判断教师信息
        int TnumberCount=teacherMapper.getTnumberCount(teacher.getTnumber());
        if(TnumberCount<1){
            //2.教师信息数据添加
            int addTeacherInt=teacherMapper.insertTeacher(teacher);
            //3.返回值设置
            returnInt=addTeacherInt;
        }else {
            returnInt=-1;//有相同的教师信息名称
        }
        return returnInt;

    }

    /**
     * 更新教师信息
     * @param teacher
     * @return 更新是否成功（-1：更新执行SQL报错）
     */
    @Override
    public int UpdateTeacher(Teacher teacher) {
        int isUpdate=0;
        //1.没有相关业务流程的判断
        try{
            isUpdate=teacherMapper.updateTeacher(teacher);
        }catch (Exception e){
            isUpdate=-1;
            //打印更新报错的情况
            System.out.print("更新教师信息时执行SQL报错的原因为："+e.getMessage());
        }
        return isUpdate;
    }

    /**
     * 教师信息删除
     * @param perid
     * @return
     */
    @Override
    public int DeleteTeacher(int perid) {
        int delTeacher=0;
        /*1.有关业务流程的判断
         * （1）删除教师不能与人员角表有关联
         * */
        try {
            //返回的结果总数量小于1，才可以删除
            if (personMapper.getTeacherCount(perid)<1){
                delTeacher=teacherMapper.delTeacher(perid);
            }
        }catch (Exception e){
            delTeacher=-1;
            System.out.print("删除角色信息室执行SQL时报错，信息为："+e.getMessage());
        }
        return delTeacher;
    }

    @Override
    public List<TeacherEOPage> getTeacherWhereAll(TeacherEOPage teacherEOPage) {
        List<TeacherEOPage> listTeacherEo=teacherMapper.getTeacherWhereAll(teacherEOPage);
        return listTeacherEo;
    }

}
